package com.zzh.takeoutapp.utils;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzh.takeoutapp.R;


public class LoadingUtil {
    private Dialog pd;
    private ImageView loading;
    private boolean showLoading;
    private Activity activity;
    private String dialogTitle;
    private TextView tv_loading;
    private boolean canCancel;
    private boolean touchCancel;

    public LoadingUtil(Activity activity) {
        this(activity, activity.getResources().getString(R.string.loading));
    }

    public LoadingUtil(Activity activity, String dialogTitle) {
        this.showLoading = true;
        this.canCancel = true;
        this.touchCancel = false;
        this.activity = activity;
        this.dialogTitle = dialogTitle;
    }

    public LoadingUtil(Activity activity, String dialogTitle, boolean canCancel) {
        this.showLoading = true;
        this.canCancel = true;
        this.touchCancel = false;
        this.activity = activity;
        this.dialogTitle = dialogTitle;
        this.canCancel = canCancel;
    }

    public void startShowLoading() {
        if (!(this.activity instanceof Activity) || !this.activity.isFinishing()) {
            if (this.showLoading && this.activity != null) {
                if (this.pd == null || !this.pd.isShowing()) {
                    this.pd = this.createDialog();
                    this.pd.show();
                }
            } else {
                this.pd = null;
            }

        }
    }

    public boolean isShowLoading() {
        return this.pd != null && this.pd.isShowing();
    }

    public void stopShowLoading() {
        if (this.pd != null && this.pd.isShowing() && this.activity != null && !this.activity.isFinishing()) {
            try {
                this.pd.dismiss();
            } catch (Exception var2) {
                var2.printStackTrace();
                return;
            }
        }

    }

    private Dialog createDialog() {
        View view = View.inflate(this.activity, R.layout.loading_layout, (ViewGroup) null);
        this.loading = (ImageView) view.findViewById(R.id.loading);
        this.tv_loading = (TextView) view.findViewById(R.id.tv_loading);
        this.tv_loading.setText(this.dialogTitle);
        Animation operatingAnim = AnimationUtils.loadAnimation(this.activity, R.anim.tip);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
        this.loading.setAnimation(operatingAnim);
        Dialog dialog = new Dialog(this.activity, R.style.update_dialog1);
        dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.setCancelable(this.canCancel);
        dialog.setCanceledOnTouchOutside(this.touchCancel);
        Window window = dialog.getWindow();
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 0.7F;
        window.setAttributes(lp);
        return dialog;
    }

    public void setCanCancel(boolean canCancel) {
        this.canCancel = canCancel;
    }

    public void setDialogTitle(String dialogTitle) {
        this.dialogTitle = dialogTitle;
        if (this.tv_loading != null) {
            this.tv_loading.setText(dialogTitle);
        }

    }
}
